package priv.abadstring.study.rocketmq.javaapi;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class AsyncProducer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("haoke");
        producer.setNamesrvAddr("abadstring.me:9876");
        producer.start();

        Message msg = new Message(
                "song-song" /* Topic */,
                "b" /* Tag */,
                ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
        );
        SendCallback callback = new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.printf("%s%n", sendResult);
                producer.shutdown();
            }
            @Override
            public void onException(Throwable ex) {
                System.out.println(ex);
                producer.shutdown();
            }
        };
        producer.send(msg, callback);
    }
}
